<template>
  <view class="result-container">
    <!-- 顶部装饰 -->
    <view class="top-decoration"></view>
    
    <!-- 结果卡片 -->
    <view class="result-card">
      <!-- 成绩展示区 -->
      <view class="score-section">
        <view class="score-circle">
          <text class="score-number">{{ score }}</text>
          <text class="score-max">/{{ totalScore }}</text>
        </view>
        <text class="score-label">你的得分</text>
      </view>
      
      <!-- 进度与正确率 -->
      <view class="progress-section">
        <view class="progress-info">
          <text>正确率</text>
          <text class="rate">{{ accuracyRate }}%</text>
        </view>
        <view class="progress-bar">
          <view 
            class="progress-fill" 
            :style="{ width: accuracyRate + '%' }"
          ></view>
        </view>
        <view class="stats">
          <view class="stat-item">
            <text class="stat-number">{{ correctCount }}</text>
            <text class="stat-label">正确</text>
          </view>
          <view class="stat-item">
            <text class="stat-number">{{ wrongCount }}</text>
            <text class="stat-label">错误</text>
          </view>
          <view class="stat-item">
            <text class="stat-number">{{ unanswerCount }}</text>
            <text class="stat-label">未答</text>
          </view>
        </view>
      </view>
      
      <!-- 评语区域 -->
      <view class="comment-section">
        <text class="comment-title">{{ commentTitle }}</text>
        <view class="comment-card">
          <text class="comment-content">{{ comment }}</text>
        </view>
      </view>
      
      <!-- 操作按钮 -->
      <view class="action-buttons">
        <button 
          class="primary-btn" 
          @click="tryAgain"
        >
          再试一次
        </button>
        <button 
          class="secondary-btn" 
          @click="viewAnalysis"
        >
          查看解析
        </button>
      </view>
    </view>
    
    <!-- 底部装饰 -->
    <view class="bottom-decoration"></view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 考试结果数据
      score: 0,               // 得分
      totalScore: 100,         // 总分
      accuracyRate: 0,        // 正确率
      correctCount: 0,        // 正确题数
      wrongCount: 0,           // 错误题数
      unanswerCount: 0,        // 未答题数
      comment: "",            // 评语内容
      commentTitle: ""        // 评语标题
    };
  },
  onLoad(options) {
    // 从URL参数中获取考试结果数据
    this.loadResultFromParams(options);
  },
  methods: {
    // 从URL参数中加载考试结果数据
    loadResultFromParams(options) {
      try {
        // 从URL参数中获取核心数据
        this.score = parseInt(options.score) || 0;
        this.correctCount = parseInt(options.correctCount) || 0;
        this.wrongCount = parseInt(options.wrongCount) || 0;
        this.totalScore = parseInt(options.totalQuestions) || 100;
        this.accuracyRate = parseFloat(options.correctRate) || 0;
        this.commentTitle = decodeURIComponent(options.commentTitle || '评语');
        this.comment = decodeURIComponent(options.commentContent || '');
        
        // 计算未答题数
        this.unanswerCount = Math.max(0, this.totalScore - this.correctCount - this.wrongCount);
        
        // 触发动画效果
        this.$nextTick(() => {
          this.startAnimation();
        });
        
      } catch (error) {
        console.error('加载结果数据失败:', error);
        uni.showToast({ 
          title: '加载结果失败', 
          icon: 'none' 
        });
      }
    },
    
    // 开始动画效果
    startAnimation() {
      // 可以在这里添加数字滚动等动画效果
    },
    
    // 再试一次
    tryAgain() {
      // 返回到测试页面
      uni.navigateBack({
        delta: 2 // 返回两级，跳过结果页面直接回到测试页面
      });
    },
    
    // 查看解析
    viewAnalysis() {
      // 跳转到解析页面
      uni.navigateTo({
        url: '/pages/analysis/analysis'
      });
    }
  }
};
</script>

<style scoped>
.result-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  position: relative;
  padding: 20px 16px;
  box-sizing: border-box;
}

/* 装饰元素 */
.top-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 120px;
  background: linear-gradient(135deg, #00cc99 0%, #00a885 100%);
  border-bottom-left-radius: 30px;
  border-bottom-right-radius: 30px;
  z-index: 1;
}

.bottom-decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  background: linear-gradient(135deg, #00cc99 0%, #00a885 100%);
  border-top-left-radius: 30px;
  border-top-right-radius: 30px;
  z-index: 1;
  opacity: 0.1;
}

/* 结果卡片 */
.result-card {
  position: relative;
  z-index: 2;
  background-color: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 30px 20px;
  margin-top: 80px;
  margin-bottom: 100px;
}

/* 成绩展示区 */
.score-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.score-circle {
  width: 160px;
  height: 160px;
  border-radius: 50%;
  background: linear-gradient(135deg, #00cc99 0%, #00a885 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 15px;
  box-shadow: 0 5px 15px rgba(0, 204, 153, 0.3);
  position: relative;
  overflow: hidden;
}

/* 圆环装饰 */
.score-circle::before {
  content: '';
  position: absolute;
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.1);
}

.score-number {
  font-size: 48px;
  font-weight: bold;
  color: #fff;
  position: relative;
  z-index: 2;
  line-height: 1;
}

.score-max {
  font-size: 18px;
  color: rgba(255, 255, 255, 0.8);
  margin-left: 5px;
  position: relative;
  z-index: 2;
}

.score-label {
  font-size: 18px;
  color: #333;
  font-weight: 500;
}

/* 进度与正确率 */
.progress-section {
  margin-bottom: 30px;
  padding: 0 10px;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 14px;
}

.rate {
  font-weight: 500;
  color: #00cc99;
}

.progress-bar {
  height: 8px;
  background-color: #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 15px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #00cc99 0%, #00a885 100%);
  border-radius: 4px;
  transition: width 1s ease-in-out;
}

.stats {
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.stat-item {
  flex: 1;
}

.stat-number {
  font-size: 18px;
  font-weight: bold;
  display: block;
  margin-bottom: 5px;
}

.stat-item:nth-child(1) .stat-number {
  color: #00cc99;
}

.stat-item:nth-child(2) .stat-number {
  color: #ff4d4f;
}

.stat-item:nth-child(3) .stat-number {
  color: #faad14;
}

.stat-label {
  font-size: 12px;
  color: #666;
}

/* 评语区域 */
.comment-section {
  margin-bottom: 30px;
  padding: 0 10px;
}

.comment-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  display: block;
  margin-bottom: 12px;
}

.comment-card {
  background-color: #f9f9f9;
  border-radius: 12px;
  padding: 15px;
  border-left: 4px solid #00cc99;
}

.comment-content {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0 10px;
}

.primary-btn, .secondary-btn {
  padding: 14px 0;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 500;
  width: 100%;
}

.primary-btn {
  background-color: #00cc99;
  color: #fff;
  border: none;
  box-shadow: 0 4px 12px rgba(0, 204, 153, 0.3);
}

.primary-btn:active {
  background-color: #00a885;
}

.secondary-btn {
  background-color: #f5f5f5;
  color: #333;
  border: none;
}

.secondary-btn:active {
  background-color: #e8e8e8;
}
</style>
